//
//  WRYMeController.m
//  BaiSiBuJie
//
//  Created by wry on 16/4/24.
//  Copyright © 2016年 WRY. All rights reserved.
//

#import "WRYMeController.h"
#import "UIBarButtonItem+WRYHighlightButton.h"
#import "WRYSettingController.h"
#import "WRYMineFootCell.h"
#import "WRYFootViewItem.h"

#import <AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#define WRYMineFootCellWH ((WRYScreenW-3)/4)

static NSString * const ID= @"cell";
@interface WRYMeController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)NSArray * squares;

@property(nonatomic,assign) CGFloat footH;
@property(nonatomic,weak)UICollectionView *collectView;
@end
     
@implementation WRYMeController

-(NSArray *)squares
{

    if (_squares==nil) {
        _squares=[NSArray array];
    }
    return _squares;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //1.设置到航条
    [self setNavcBarItem];
    
    self.view.backgroundColor=WRYColor(206, 206, 206);
    
    [self setupFootView];
    
    [self loadData];
}

#pragma mark ----------
#pragma mark - 请求数据
-(void)loadData
{
    //1.
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    
    NSMutableDictionary *parameter=[NSMutableDictionary dictionary];
    parameter[@"a"]=@"square";
    parameter[@"c"]=@"topic";

    
    [mgr GET:@"http://api.budejie.com/api/api_open.php" parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //解析数据
        [responseObject writeToFile:@"/Users/wang/Desktop/iOS/04-项目准备/wry-练习/square.plist" atomically:YES ];
        NSArray *arr=responseObject[@"square_list"];

      _squares= [WRYFootViewItem mj_objectArrayWithKeyValuesArray:arr];
    
        [self.collectView  reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}

#pragma mark ----------
#pragma mark - 设置底部视图
-(void)setupFootView
{
    //1.创建布局
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    
    // 设置cell尺寸
    layout.minimumLineSpacing=1;
    layout.minimumInteritemSpacing=1;
    
    layout.itemSize=CGSizeMake((WRYScreenW-3)/4,(WRYScreenW-3)/4);
    
    //2.创建UICollectionView
    UICollectionView *collectView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WRYScreenW,300 ) collectionViewLayout:layout];
    
    //3.注册cell
    [collectView registerNib:[UINib nibWithNibName:@"WRYMineFootCell" bundle:nil] forCellWithReuseIdentifier:ID ];
    
    collectView.backgroundColor = self.tableView.backgroundColor;
    
    _collectView=collectView;
    
    collectView.showsVerticalScrollIndicator=NO;
    collectView.showsHorizontalScrollIndicator=NO;
    collectView.scrollEnabled=NO;
    
    //设置tableView底部视图
    self.tableView.tableFooterView=collectView;
    
    collectView.dataSource=self;
    collectView.delegate=self;

}
#pragma mark ----------
#pragma mark - UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return _squares.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    WRYMineFootCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    cell.item=self.squares[indexPath.row];
    
    return cell;
}


#pragma mark ----------
#pragma mark - 导航条内容

//1.设置导航条内容
-(void)setNavcBarItem

{
    //"设置"按钮
    UIBarButtonItem *itemSetting=[UIBarButtonItem itemWithNormalImage:[UIImage imageNamed:@"mine-setting-icon"] highlightImage:[UIImage imageNamed:@"mine-setting-icon-click"] target:self action:@selector(itemSettingClick)];
    //夜间模式按钮
    UIBarButtonItem *itemNight=[UIBarButtonItem itemWithNormalImage:[UIImage imageNamed:@"mine-moon-icon"]  selectImage:[UIImage imageNamed:@"mine-sun-icon-click"] target:self action:@selector(itemNightClick:)];
    
    self.navigationItem.title=@"我的";
    self.navigationItem.rightBarButtonItems=@[itemSetting,itemNight];
    
}

//点击"设置"按钮
-(void)itemSettingClick
{
    WRYSettingController * vc=[[WRYSettingController alloc]init];
    
    //注意：一定要在push之前去设置 隐藏tabbar
//    self.hidesBottomBarWhenPushed=YES;
    //跳转
    [self.navigationController pushViewController:vc animated:YES];

}

//夜间模式按钮点击
-(void)itemNightClick:(UIButton *)itemitemNight
{
    itemitemNight.selected=!itemitemNight.selected;
}


@end
